long curMillis; 
double alpha=0.75; 
int period=20;
double change=0.0;
void setup()
{
 Serial.begin(9600);
}
void loop()
{
 static long preMillis=millis();
 static int number = 0;
 static double oldValue=0;
 static double oldChange=0;
 int rValue=analogRead(A0); //读取传感器的值
 double value=alpha*oldValue+(1-alpha)*rValue;
 change=value-oldValue;
 if(change<0.0&&oldChange>0.0)
 {
  number++;
 }

 oldValue=value;
 oldChange=change;
 delay(period);
 curMillis=millis();
 if(curMillis - preMillis >= 20000)
 {
  Serial.print("Your pulse in 20 seconds is ");
  Serial.println(number);
  number=0;
  curMillis=0;
  preMillis=millis();
 }
}

